<template>
	<div>
		<y-shelf title="我的订单">
			<div slot="content">
				<div v-if="orderList.length">
					<div v-for="(item,i) in orderList" :key="i">
						<div class="gray-sub-title cart-title">
							<div class="first">
								<div>
									<span class="date" v-text="item.UpdateDate"></span>
									<span class="order-id"> 订单号： <a href="javascript:;" style="cursor: default;">{{item.Id}}</a> </span>
								</div>
								<div class="f-bc">
									<span class="price">单价</span>
									<span class="num">数量</span>
									<span class="operation">商品操作</span>
								</div>
							</div>
							<div class="last">
								<span class="sub-total">实付金额</span>
								<!--<span class="sub-total">状态</span>-->
								<span class="order-detail"> <a @click="orderDetail(item.Id)">查看详情<em class="icon-font"></em></a> </span>
							</div>
						</div>
						<div class="pr">
							<div class="cart" v-for="(good,j) in item.ProductsList" :key="j">
								<div class="cart-l" :class="{bt:j>0}">
									<div class="car-l-l">
										<div class="img-box">
											<img :src="root + good.ImgPath" alt="">
										</div>
										<div class="ellipsis">
											{{good.ProductName}}
										</div>
									</div>
									<div class="cart-l-r">
										<div>¥ {{good.SalePrice}}</div>
										<div class="num">{{item.Quantity}}</div>
										<div class="type">
											<!--<a @click="_delOrder(item.Id)" href="javascript:;" v-if="item.OrderStatus==5||item.OrderStatus==0" class="del-order">删除此订单</a>-->
											<a @click="orderPayment(item)" href="javascript:;" v-if="item.OrderStatus==1" class="del-order">现在付款</a>
											<a @click="_cancelOrder(item)" href="javascript:;" v-if="item.OrderStatus==1" class="del-order">取消订单</a>
											<a @click="_sureGet(item)" href="javascript:;" v-if="item.OrderStatus==3" class="del-order">确认收货</a>
											<a @click="_pingJiaOrder(item)" href="javascript:;" v-if="item.OrderStatus==4&&good.IsRated==0" class="del-order">评价订单</a>
											<el-button @click="_delOrder(item.Id,i)" type="danger" size="small" v-if="item.OrderStatus==4&&good.IsRated==1" class="del-order">删除此订单</el-button>
											<el-button @click="_delOrder(item.Id,i)" type="danger" size="small" v-if="item.OrderStatus==5||item.OrderStatus==0" class="del-order">删除此订单</el-button>
										</div>
									</div>
								</div>
								<div class="cart-r">
									<span></span>
									<span></span>
								</div>
							</div>
							<div class="prod-operation pa" style="right: 0;top: 0;">
								<div class="total">¥ {{item.OrderTotal}}</div>
								<div class="status"> {{item.OrderStatusText}} </div>
							</div>
						</div>
					</div>
				</div>
				<div v-else>
					<div style="padding: 100px 0;text-align: center">
						你还未创建过订单
					</div>
				</div>
			</div>
		</y-shelf>
		<div style="float:right">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 20, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next" :total="total">
			</el-pagination>
		</div>
		<el-dialog title="取消订单" :visible.sync="dialogVisible" width="30%">
			<span>取消原因:</span>
			<el-select v-model="value" placeholder="请选择">
				<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
				</el-option>
			</el-select>
			<span slot="footer" class="dialog-footer">
	    		<el-button @click="dialogVisible = false">取 消</el-button>
	    		<el-button type="primary" @click="_cancelOrderSure">确 定</el-button>
  			</span>
		</el-dialog>
	</div>
</template>
<script>
	import { orderList, delOrder, cancelOrder, SureGetGoods } from '/api/goods'
	import YShelf from '/components/shelf'
	import { getStore, setStore } from '/utils/storage'

	export default {
		data() {
			return {
				orderList: [],
				currentPage: 1,
				pageSize: 20,
				total: 0,
				dialogVisible: false,
				options: [{
					value: '选错商品了',
					label: '选错商品了'
				}, {
					value: '不想要了',
					label: '不想要了'
				}],
				value: '',
				cancelOrderId: ''

			}
		},
		methods: {
			goodsDetails(id) {
				window.open(window.location.origin + '/pc#/goodsDetails?productId=' + id)
			},
			orderPayment(item) {
				console.log('item', item)
				setStore('setTime', 90)
				this.$router.push({
					path: '/order/wechat',
					query: {
						price: item.OrderTotal,
						OrderId: item.Id
					}
				})
				//				window.open(window.location.origin + '#/order/payment?orderId=' + orderId)
			},
			_orderList() {
				let arrayList = []
				let OrderStatusText = ''
				let params = {
					isdelete: 0,
					limit: this.pageSize,
					page: this.currentPage,
				}
				orderList(params).then(res => {
					console.log('获取订单列表res', res)
					if(res.code === 200) {
						this.total = res.data.dataCount
						this.orderList = res.data.list
						arrayList = this.orderList
						for(var i = 0; i < arrayList.length; i++) {
							switch(arrayList[i].OrderStatus) {
								case 0:
									OrderStatusText = '已取消'
									break;
								case 1:
									OrderStatusText = '待付款'
									break;
								case 2:
									OrderStatusText = '待发货'
									break;
								case 3:
									OrderStatusText = '待收货'
									break;
								case 4:
									OrderStatusText = '已完成'
									break;
								case 5:
									OrderStatusText = '已完成'
									break;
								default:
									break;
							}
							this.$set(this.orderList[i], 'OrderStatusText',
								OrderStatusText)
						}
					}
				})
			},
			_delOrder(orderId, i) {
				let me = this
				delOrder(orderId).then(res => {
					if(res.code === 200) {
						//						this.orderList.splice(i, 1)
						me._orderList()

					} else {
						me.$message({
							message: res.error,
							type: 'warning'
						})
					}
				})
			},
			_cancelOrder(item) {
				console.log('取消订单 item', item)
				this.dialogVisible = true
				this.cancelOrderId = item.Id
			},
			_cancelOrderSure() {
				let me = this
				cancelOrder(this.cancelOrderId, {
					OrderStatus: 0,
					CloseReason: this.value
				}).then(res => {
					console.log('取消订单返回res', res)
					if(res.code === 200) {
						me.dialogVisible = false
						me._orderList()
					} else {
						me.$message({
							message: res.error,
							type: 'warning'
						})
					}
				})
			},
			_pingJiaOrder(item) {
				this.$router.push({
					path: 'orderRate',
					query: {
						orderId: item.Id
					}
				})
			},
			handleSizeChange(val) {
				this.pageSize = val
				this._orderList()
			},
			handleCurrentChange(val) {
				this.currentPage = val
				this._orderList()
			},
			orderDetail(orderId) {
				this.$router.push({
					path: 'orderDetail',
					query: {
						orderId: orderId
					}
				})
			},
			_sureGet(item) {
				let me = this
				this.$MessageBox('确定收货, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					SureGetGoods(item.Id, {OrderStatus: 4}).then(res => {
						console.log('确定收货放回res', res)
						if(res.code===200) {
							me._orderList()
						}
					})
//					this.$message({
//						type: 'success',
//						message: '删除成功!'
//					});
				})
			}
		},
		created() {
			this._orderList()
		},
		components: {
			YShelf
		},
		computed: {
			//  	OrderStatusText: function() {
			//  		
			//  	}
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../../assets/style/mixin";
	.gray-sub-title {
		height: 38px;
		padding: 0 24px;
		background: #EEE;
		border-top: 1px solid #DBDBDB;
		border-bottom: 1px solid #DBDBDB;
		line-height: 38px;
		font-size: 12px;
		color: #666;
		display: flex;
		span {
			display: inline-block;
			height: 100%;
		}
		.first {
			display: flex;
			justify-content: space-between;
			flex: 1;
			.f-bc {
				>span {
					width: 112px;
					text-align: center;
				}
			}
		}
		.last {
			width: 230px;
			text-align: center;
			display: flex;
			border-left: 1px solid #ccc;
			span {
				flex: 1;
			}
		}
	}
	
	.bt {
		border-top: 1px solid #EFEFEF;
	}
	
	.date {
		padding-left: 6px;
	}
	
	.order-id {
		margin-left: 20px;
	}
	
	.cart {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 24px;
		&:hover {
			.del-order {
				display: block;
			}
		}
		.del-order {
			display: none;
		}
		.cart-l {
			display: flex;
			align-items: center;
			flex: 1;
			padding: 15px 0;
			justify-content: space-between;
			position: relative;
			&:before {
				position: absolute;
				content: ' ';
				right: -1px;
				top: 0;
				width: 1px;
				background-color: #EFEFEF;
				height: 100%;
			}
			.ellipsis {
				margin-left: 20px;
				width: 220px;
			}
			.img-box {
				border: 1px solid #EBEBEB;
			}
			img {
				display: block;
				@include wh(80px);
			}
			.cart-l-r {
				display: flex;
				>div {
					text-align: center;
					width: 112px;
				}
			}
			.car-l-l {
				display: flex;
				align-items: center;
			}
		}
		.cart-r {
			width: 230px;
			display: flex;
			span {
				text-align: center;
				flex: 1;
			}
		}
	}
	
	.prod-operation {
		height: 110px;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 254px;
		div {
			width: 100%;
			text-align: center;
		}
		div:last-child {
			padding-right: 24px;
		}
	}
</style>